import React, { memo, useState, useEffect } from 'react'
import { Space, Table, Tag, Button, message } from 'antd';
import { getdraf } from '../../../api/home'
import Item from 'antd/es/list/Item';
import { useLocation } from "react-router-dom";
import { startuse } from '../../../api/startblog';
import { forbiddens} from '../../../api/forbidden';
const check = memo(() => {
    const [current, setCurrent] = useState(1);
    const [pageSize, setPageSize] = useState(8);
    const [dataList, setDatalist] = useState([]);//列表数据
    let location = useLocation();
    console.log("params:", location);


    // 请求发布箱
    const getdrafs = () => {
        let info = JSON.parse(localStorage.params)
        let parmas = {
            category: info.category,
            page: info.page,
            pagesize: info.pagesize,
            searchKey: info.searchKey,
            state: location.state.state
        }
        // console.log(parmas);
        getdraf(parmas).then(res => {
            // console.log(res);
            let data = res.data.result.data.map((item, i) => {
                return {
                    key: i,
                    name: item.author,
                    content: item.content,
                    title: item.title,
                    id: item.id,
                    mainImage: item.mainImage
                }
            });
            // console.log(data);
            setDatalist(data)
        })
    }

    //启动博客
    const startblog = (index) => {
        console.log(dataList[index]);
        let params = {
            Id: dataList[index].id
        }
        startuse(params).then(res => {
            console.log('启用日志',res);
            if(res.response.data.success==true){
                message.success('启用成功');
                getdrafs()
            }else{
                message.error(res.response.data.errorMessage);
            }
            // message.success('启用成功');
            // getdrafs()
        }
        )
    }
    //禁用博客
    const forbiddenblog = (index) => {
        console.log(dataList[index]);
        let params = {
            Id: dataList[index].id
        }
        forbiddens(params).then(res => {
            console.log('启用日志',res);
            if(res.response.data.success==true){
                message.success('禁用成功');
                getdrafs()
            }else{
                message.error(res.response.data.errorMessage);
            }
        }
        )
    }

    useEffect(() => {
        getdrafs()
    }, [])

    const columns = [
        {
            title: '名称',
            dataIndex: 'name',
            align: 'center',
            key: 'name',
            render: (text) => <a>{text}</a>,
        },
        {
            title: '编辑内容',
            dataIndex: 'content',
            key: 'content',
            align: 'center',
        },
        {
            title: '编辑标题',
            key: 'title',
            dataIndex: 'title',
            align: 'center',
        },
        {
            title: '操作',
            key: 'action',
            dataIndex: 'action',
            align: 'center',
            render: (_, record, index) => (
                <>
                    <Button onClick={() => startblog(index)} type="primary">启动</Button>
                    <Button style={{ marginLeft: '10px' }} onClick={() => forbiddenblog(index)} type="primary">禁用</Button>
                </>
            ),
        },
    ];

    return (
        <div style={{ background: '#fff', borderRadius: '10px', padding: '10px', height: '100%' }}>
            <Table
                bordered
                columns={columns}
                dataSource={dataList}
                pagination={{
                    current,
                    pageSize,
                    total: dataList.length,
                    showTotal(totla, range) {
                        return `共 ${totla} 条`
                    },
                    onChange(page, pageSize) {
                        console.log(page, pageSize)
                        setCurrent(page)
                        setPageSize(pageSize)
                    }
                }} />
        </div>
    )
})

export default check
